package service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.Film;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Class with the implementation of the custom repository code. Uses JDBC in
 * this case. For basic programatic setup see {@link FilmRepositoryImpl} for
 * examples.
 * <p>
 * As you need to hand the instance a {@link javax.sql.DataSource} or
 * {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate} you manually
 * need to declare it as Spring bean:
 * 
 * <pre>
 * &lt;jpa:repository base-package=&quot;com.acme.repository&quot; /&gt;
 * 
 * &lt;bean id=&quot;filmRepositoryImpl&quot; class=&quot;...DatentraegerRepositoryJdbcImpl&quot;&gt;
 *   &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
 * &lt;/bean&gt;
 * </pre>
 * 
 * Using {@code filmRepositoryImpl} will cause the repository instance get this
 * bean injected for custom repository logic as the default postfix for custom
 * DAO instances is {@code Impl}.
 * 
 * @author Andre
 */
class FilmRepositoryJdbcImpl extends JdbcDaoSupport implements
		FilmRepositoryDatentraeger {

	private static final String COMPLICATED_SQL = "SELECT * FROM Datentraeger";

	/*
	 * (non-Javadoc)
	 * 
	 * @see sevice.impl.DatentraegerRepositoryFilmVerwaltung#
	 * filmVerwaltungBatchOperation()
	 */
	public List<Film> datentraegerBatchOperation() {

		return getJdbcTemplate().query(COMPLICATED_SQL, new FilmRowMapper());
	}

	private static class FilmRowMapper implements ParameterizedRowMapper<Film> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.springframework.jdbc.core.simple.ParameterizedRowMapper#mapRow
		 * (java.sql.ResultSet, int)
		 */
		public Film mapRow(ResultSet rs, int rowNum) throws SQLException {

			Film film = new Film(rs.getString("Name"));
			film.setName(rs.getString("Name"));
			film.setHauptdarsteller(rs.getString("Hauptdarsteller"));
			film.setGenre(rs.getString("Genre"));
			film.setLaenge(rs.getInt(110));

			return film;
		}
	}
}
